package com.yachang.qym.usbhost.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.model.RouteModel;
import com.yachang.qym.usbhost.utils.AllData;

public class RouteActivity extends AppCompatActivity {
    private TextView tvSendName;
    private TextView tvRouteName;
    private TextView tvRouteStyle;
    private TextView tvRouteOtherName;
    private TextView tvCalledName;
    private RelativeLayout layoutCalled;
    private RouteModel mRouteModel;

    private Toolbar mToolbar;
    private int mRouteID;
    private int mDmrID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route);
        Intent intent = getIntent();
        mRouteID = intent.getIntExtra("RouteID", 0);
        mDmrID = intent.getIntExtra("DmrID", 0);
        initView();
        bingData(mRouteID);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tvSendName = (TextView) findViewById(R.id.tv_send_name);
        tvRouteName = (TextView) findViewById(R.id.tv_now_route);
        tvRouteStyle = (TextView) findViewById(R.id.tv_call_style);
        layoutCalled = (RelativeLayout) findViewById(R.id.layout_called_list);
        tvRouteOtherName = (TextView) findViewById(R.id.tv_route_other_name);
        tvCalledName = (TextView) findViewById(R.id.tv_called_name);
        layoutCalled.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RouteActivity.this, CallerActivity.class);
                intent.putExtra("calledID", AllData.routeModels.get(mRouteID).getCalledsID());
                startActivity(intent);
            }
        });
    }

    private void bingData(int routeID) {
        mRouteModel = AllData.routeModels.get(routeID);
        tvRouteName.setText(mRouteModel.getName());
        getSupportActionBar().setTitle(mRouteModel.getName());
        tvRouteOtherName.setText("DMR/CH" + (mDmrID + 1));
        tvSendName.setText(AllData.contactModels.get(mRouteModel.getCallerID()).getName());
        if(mRouteModel != null) {
            if((AllData.routeModels.get(mRouteID).getCalledsID() - 1) >= 0){
                tvCalledName.setText(AllData.calledModels.get(AllData.routeModels.get(mRouteID).getCalledsID() - 1).getName());
                int callStyle = AllData.contactModels.get(mRouteModel.getCallerID()).getStyle();
                switch (callStyle) {
                    case 0:
                        tvRouteStyle.setText("组呼");
                        break;
                    case 1:
                        tvRouteStyle.setText("个呼");
                        break;
                    case 2:
                        tvRouteStyle.setText("全呼");
                        break;
                }
            }
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                RouteActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
